﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using HtmlAgilityPack;
using System.Xml;

namespace Lab2
{
    public class ChangeUnits
    {
        public enum TypeChange
        {
            USD2VND = 1, 
            VND2USD = 2, 
            EUR2VND = 3,
            VND2EUR = 4, 
            YP2VND = 5, 
            VND2YP = 6 
        }
        //Lay ty gia truc tuyen tu vietcombank
        public List<Rate> VietComBank()
        {

            XmlDocument xml = new XmlDocument();
            
            List<Rate> rates = new List<Rate>();

            string html = "";
            using (WebClient client = new WebClient())
            {
                //html = System.Text.Encoding.UTF8.GetString(client.DownloadData("http://www.vietcombank.com.vn/exchangerates/ExrateXML.aspx"));
                html = System.Text.Encoding.UTF8.GetString(client.DownloadData("http://localhost/Rate/rate.html"));
            }

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            string buy, trans, sell, currentcode;
            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//Exrate"))
            {
                //buy = node.Attributes["Buy"].Value;
                //trans = node.Attributes["Transfer"].Value;
                //sell = node.Attributes["Sell"].Value;
                //currentcode = node.Attributes["CurrencyCode"].Value;

                //Rate rate = new Rate(buy, trans, sell, currentcode);
                //rates.Add(rate);
            }
            return rates;
        }
        //01/06/2012
        public decimal Change(decimal money, TypeChange type)
        {
            List<Rate> list = VietComBank();

            decimal usd2vnd = Decimal.Parse(list.Find(p => p.CurrentCode == "USD").Buy);
            decimal vnd2usd = Decimal.Parse(list.Find(p => p.CurrentCode == "USD").Sell);
            decimal eur2vnd = Decimal.Parse(list.Find(p => p.CurrentCode == "EUR").Buy);
            decimal vnd2eur = Decimal.Parse(list.Find(p => p.CurrentCode == "EUR").Sell);
            decimal jpy2vnd = Decimal.Parse(list.Find(p => p.CurrentCode == "JPY").Buy);
            decimal vnd2jpy = Decimal.Parse(list.Find(p => p.CurrentCode == "JPY").Sell);

            if (type == TypeChange.USD2VND)
            {
                return money * usd2vnd;
            }
            else if (type == TypeChange.VND2USD)
            {
                return money / vnd2usd;
            }
            else if (type == TypeChange.EUR2VND)
            {
                return money * eur2vnd;
            }
            else if (type == TypeChange.VND2EUR)
            {
                return money / vnd2eur;
            }
            else if (type == TypeChange.YP2VND)
            {
                return money * jpy2vnd;
            }
            else
                return money / vnd2jpy;
        }

    }
}